//
//  1. 两数之和.swift
//  Top600
//
//  Created by rjb on 2021/8/6.
//

import Foundation

class Solution1 {
    // a + b = 9
    // b = 9 - a
    // 即 9 - a 在hash表里是否存在即可
    func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
        // value为key,index为value,
        var map: [Int: Int] = [:]
        for i in 0..<nums.count {
            let value: Int = target - nums[i]
            if let old = map[value] {
                return [old, i]
            } else {
               map[nums[i]] = i
            }
        }
        return []
    }
    static func test() {
        let solution = Solution1()
        let nums = [3, 3]
        let result = solution.twoSum(nums, 6)
        print(result)
    }
}
